﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Core;
using Web.Prop;

namespace Web
{
    public partial class register1 : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            try
            {

                if (String.IsNullOrEmpty(txUsername.Text.Trim()))
                {
                        Alert.Message(this.Page, "กรุณากรอกชื่อ");
                }
                else
                {
                    var o = Dao.USERDao.Init().Select(
                                                       new USER()
                                                       {
                                                           UWD = txUsername.Text.Trim()
                                                       }
                                                       );
                    if (o.Count > 0)
                    {
                        Alert.Message(this.Page, "ชื่อนี้ล็อกอินนี้ถูกใช้ไปแล้ว");
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(txPassword.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกรหัสผ่าน");
                        }
                        else if (String.IsNullOrEmpty(txConfirm.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณายืนยันรหัสผ่าน");
                        }

                        else if (!txPassword.Text.Trim().Equals(txConfirm.Text.Trim()))
                        {
                            Alert.Message(this.Page, "รหัสผ่านไม่ตรงกัน");
                        }
                        else if (String.IsNullOrEmpty(txFName.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกชื่อ");
                        }
                        else if (String.IsNullOrEmpty(txLNAME.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกนามสกุล");
                        }
                        else if (String.IsNullOrEmpty(txEmail.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอก Email");
                        }
                        //else if ((LoginDal.Init().GetPassword(txEmail.Text.Trim())).Rows.Count > 0)
                        else if (Dao.USERDao.Init().Select(new USER() { EMAIL = txEmail.Text.Trim() }).Count > 0)
                        {
                            Alert.Message(this.Page, "Email นี้ถูกใช้ไปแล้ว");
                        }
                        else if (String.IsNullOrEmpty(txTel.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกเบอร์โทร");
                        }
                        else if (String.IsNullOrEmpty(txCompany.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกชื่อบริษัท");
                        }
                        else if (String.IsNullOrEmpty(txDeparment.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกชื่อแผนกงาน");
                        }
                        else if (String.IsNullOrEmpty(txPosition.Text.Trim()))
                        {
                            Alert.Message(this.Page, "กรุณากรอกชื่อตำแหน่งงาน");
                        }
                        else
                        {
                            String UID = Guid.NewGuid().ToString().Replace("-", String.Empty);
                            Boolean result = Dao.USERDao.Init().Add(new USER()
                            {
                                UID = UID
                                ,
                                UWD = txUsername.Text.Trim()
                                ,
                                PWD = Core.Cipher.EncrytData(txPassword.Text.Trim())
                                ,
                                FNAME = txFName.Text.Trim()
                                ,
                                LNAME = txLNAME.Text.Trim()
                                ,
                                EMAIL = txEmail.Text.Trim()
                                ,
                                TEL = txTel.Text.Trim()
                                ,
                                COMPANY = txCompany.Text.Trim()
                                ,
                                DEPT = txDeparment.Text.Trim()
                                ,
                                POSITION = txPosition.Text.Trim()
                                ,
                                STATUS =  ((Int32)Core.Status.Register).ToString()
                                ,
                                CRDARE = DateTime.Now
                                ,
                                LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                                ,
                                AUTH = ((Int32)Core.Authorize.User).ToString()
                            });

                            if (!result)
                            {
                                
                            //
                                Dao.UserlogDao.Init().Add(new Userlog()
                                    {
                                        UID = UID
                                        ,
                                        LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                                        ,
                                        UPDATE = DateTime.Now
                                        ,
                                        EVENT = Core.Status.Register.ToString()
                                    });

                                Response.Redirect("singin.aspx", false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = null
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }

            upl.Update();
            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("singin.aspx");
            upl.Update();
        }
    }
    }
